package com.example.d6_tcp3;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端
 */
public class Client {
    public static void main(String[] args) throws IOException {
        System.out.println("客户端启动...");
        Socket socket = new Socket("127.0.0.1", 8888);
        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("输入要发送的数据：");
            String msg = scanner.nextLine();
            if ("exit".equals(msg)) {
                System.out.println("客户端退出...");
                dataOutputStream.close();
                socket.close();
                break;
            }
            dataOutputStream.writeUTF(msg);
            dataOutputStream.flush();
        }
    }
}
